package com.orange.controller.expert;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.controller.BaseController;
import com.orange.dbhelp.page.Page;
import com.orange.handler.AuthExportGroupMapService;
import com.orange.handler.expert.AuthExpertAdviceService;
import com.orange.handler.expert.AuthExpertItemMapService;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.expert.AuthExpertAdvice;
import com.orange.modules.entity.expert.AuthExpertItemMap;
import com.orange.modules.entity.expert.AuthExportGroupMap;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/expertAdvice")
public class AuthExpertAdviceController extends BaseController{

	private static final Logger log = Logger.getLogger(AuthExpertAdviceController.class);
	
	@Autowired
	private AuthExpertAdviceService authExpertAdviceService;
	
	@Autowired
	private AuthExpertItemMapService authExpertItemMapService;
	
	@Autowired
	private AuthExportGroupMapService authExportGroupMapService;
	
	/**
	 * 材料评审--保存专家建议
	 */
	@RequestMapping(value = "/saveAuthExpertAdviceMaterial", produces = MediaType.APPLICATION_JSON_VALUE+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveAuthExpertAdviceMaterial(HttpServletRequest request,@RequestBody Map<String, String> map){
		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(StringUtils.isBlank(expertId)){
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"未登录");
		}
		try {
			AuthExpertItemMap aei = authExpertItemMapService.queryGroupIdByExpertId(expertId);
			String groupId = aei.getGroupId();
			
			String keyword = map.get("keyWords");
			String detailedAdvice = map.get("suggestions");
			
			String[] keywords = keyword.split("#");
			String[] detailedAdvices = detailedAdvice.split("#");
			
			
			for (int i = 0; i < detailedAdvices.length; i++) {
				AuthExpertAdvice aea = new AuthExpertAdvice();
				aea.setGroupId(groupId);
				aea.setExpertId(expertId);
				aea.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				if(detailedAdvices[i].equals(" ")){
					aea.setDetailedAdvice(null);
				}else{
					aea.setDetailedAdvice(detailedAdvices[i]);
				}
				if(keywords[i].equals(" ")){
					aea.setKeyword(null);
				}else{
					aea.setKeyword(keywords[i]);
				}
				authExpertAdviceService.saveAuthExpertAdvice(aea);
			}

			return toResponse(null, ReturnCode.SUCCESS, "保存成功!");
		} catch (SQLException e) {
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "保存失败!");
		}
		
	} 
	
	/**
	 * 材料评审--查询专家建议
	 */
	@RequestMapping(value="/queryAuthExpertAdviceMaterial" ,produces = MediaType.TEXT_HTML_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryAuthExpertAdviceMaterial(HttpServletRequest request,HttpServletResponse response){
		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(StringUtils.isBlank(expertId)){
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"未登录");
		}
		AuthExpertItemMap aei;
		try {
			aei = authExpertItemMapService.queryGroupIdByExpertId1(expertId);
			String groupId = aei.getGroupId();
			List<AuthExpertAdvice> aea = authExpertAdviceService.queryAuthExpertAdviceByExpertIdAndGroupId(expertId, groupId);
			return toResponse(aea, ReturnCode.SUCCESS, "查询成功!");
		} catch (SQLException e) {
			
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "查询失败!");
		}
		
	}
	
	/**
	 * 现场考察--保存专家建议
	 */
	@RequestMapping(value = "/saveAuthExpertAdviceObservation", produces = MediaType.APPLICATION_JSON_VALUE+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveAuthExpertAdviceObservation(HttpServletRequest request,@RequestBody Map<String, String> map){
		
		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(StringUtils.isBlank(expertId)){
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"未登录");
		}
		AuthExportGroupMap eg;
		try {
			eg = authExportGroupMapService.querygroupIdByExpertId(expertId);
			String groupId = eg.getGroupId();
			
			String keyword = map.get("keyWords");
			String detailedAdvice = map.get("suggestions");
			
			String[] keywords = keyword.split("#");
			String[] detailedAdvices = detailedAdvice.split("#");
			
			for (int i = 0; i < detailedAdvices.length; i++) {
				AuthExpertAdvice aea = new AuthExpertAdvice();
				aea.setGroupId(groupId);
				aea.setExpertId(expertId);
				aea.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				if(detailedAdvices[i].equals(" ")){
					aea.setDetailedAdvice(null);
				}else{
					aea.setDetailedAdvice(detailedAdvices[i]);
				}
				if(keywords[i].equals(" ")){
					aea.setKeyword(null);
				}else{
					aea.setKeyword(keywords[i]);
				}
				authExpertAdviceService.saveAuthExpertAdvice(aea);
			}
			
			return toResponse(null, ReturnCode.SUCCESS, "保存成功!");
		} catch (SQLException e) {
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "保存失败!");
		}
		
	}
	
	/**
	 * 现场考察--查询专家建议
	 */
	@RequestMapping(value="/queryAuthExpertAdviceObservation" ,produces = MediaType.TEXT_HTML_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryAuthExpertAdviceObservation(HttpServletRequest request,HttpServletResponse response){
		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(StringUtils.isBlank(expertId)){
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"未登录");
		}
		AuthExportGroupMap eg;
		try {
			eg = authExportGroupMapService.querygroupIdByExpertId(expertId);
			String groupId = eg.getGroupId();
			List<AuthExpertAdvice> aea = authExpertAdviceService.queryAuthExpertAdviceByExpertIdAndGroupId(expertId, groupId);
			return toResponse(aea, ReturnCode.SUCCESS, "查询成功!");
		} catch (SQLException e) {
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "查询失败!");
		}
		
	}
	
	/**
	 * 管理员--分页查询专家建议
	 */
	@RequestMapping(value = "/queryAuthExpertAdviceByPage", produces = MediaType.APPLICATION_JSON_VALUE+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthExpertAdviceByPage(HttpServletRequest request,@RequestBody Map<String, String> map){
		String pageSize = map.get("pageSize");
		String pageNumber = map.get("pageNumber");
		if(StringUtils.isBlank(pageNumber) || StringUtils.isBlank(pageSize)){
			return toResponse(null, ReturnCode.FAIL, "参数存在空值!");
		}
		try {
			Page<AuthExpertAdvice> page = authExpertAdviceService.queryAuthExpertAdviceByPage(map);
			return toResponse(page, ReturnCode.SUCCESS, "分页查询成功!");
		} catch (SQLException e) {
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "分页查询失败");
		}
		
	}
	
	/**
	 * 管理员--查询专家建议详情
	 */
	@RequestMapping(value="/queryAuthExpertAdvice" ,produces = MediaType.TEXT_HTML_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryAuthExpertAdvice(HttpServletRequest request,HttpServletResponse response,@RequestParam String expertId,@RequestParam String groupId){
		try {
			List<AuthExpertAdvice> aea = authExpertAdviceService.queryAuthExpertAdviceByExpertIdAndGroupId(expertId, groupId);
			return toResponse(aea, ReturnCode.SUCCESS, "查询成功!");
		} catch (SQLException e) {
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "查询失败!");
		}
		
	}
}
